package com.kayt.cyberBar.statistics.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.ServerResponse;
import com.kayt.cyberBar.statistics.dao.impl.StatisticsTypeSalesCyberMonthDaoImpl;
import com.kayt.cyberBar.statistics.entity.po.BStatisticsTypeSalesCyberMonth;
import com.kayt.cyberBar.statistics.entity.vo.StatisticsTypeQueryVO;
import com.kayt.cyberBar.statistics.service.IStatisticsTypeSalesCyberMonthService;

@Service("statisticsTypeSalesCyberMonthService")
public class StatisticsTypeSalesCyberMonthServiceImpl implements IStatisticsTypeSalesCyberMonthService {

	@Autowired
	private StatisticsTypeSalesCyberMonthDaoImpl typeSalesCyberDao;

	@Override
	public ServerResponse<PageData<BStatisticsTypeSalesCyberMonth>> getList(StatisticsTypeQueryVO reqVO) {
		try {
			String sql = " o.statisticsTime >= ?";
			sql += " and o.statisticsTime <= ?";
			List<String> params = new ArrayList<>();
			params.add(reqVO.getStartDate());
			params.add(reqVO.getEndDate());
			if (StringUtils.isNotEmpty(reqVO.getLevel())) {
				sql += " o.level = ?";
				params.add(reqVO.getLevel());
			}
			if (StringUtils.isNotEmpty(reqVO.getTypeName())) {
				sql += " and o.typeName like %?%";
				params.add(reqVO.getTypeName());
			}
			if (StringUtils.isNotEmpty(reqVO.getParentTypeCode())) {
				sql += " and o.parentTypeCode = ?";
				params.add(reqVO.getParentTypeCode());
			}
			if (StringUtils.isNotEmpty(reqVO.getStoreCode())) {
				sql += " and o.cyberCode = ?";
				params.add(reqVO.getStoreCode());
			}
			LinkedHashMap<String, String> orderMap = new LinkedHashMap<>();
			if (StringUtils.isNotEmpty(reqVO.getSortName())) {
				orderMap.put(reqVO.getSortName(), reqVO.getSortOrder());
			} else {
				orderMap.put("id", "desc");
			}
			PageData<BStatisticsTypeSalesCyberMonth> pageData = typeSalesCyberDao.findPage(reqVO.getPageNum(),
					reqVO.getPageSize(), sql, params.toArray(), orderMap);
			return ServerResponse.createBySuccess("success.", pageData);
		} catch (SQLException e) {
			e.printStackTrace();
			PageData<BStatisticsTypeSalesCyberMonth> pageData = new PageData<>();
			return ServerResponse.createByError("fail.", pageData);
		}
	}
}
